/**
 * 植物的属请求
 * */
const {
    express,
    db_mysql,
    errorData,
    strLen
} = require('./common-req.js')
const router = express.Router();
/**
 * 获取全部的属
 * */
router.get('/all', (req, res) => {
    let countSql = 'select count(*) as count from genus';
    let doorSql = 'select * from genus;'
    db_mysql.select(countSql, null, result => {
        if (result && result.length > 0) {
            let resData = {
                data: [],
                status: 0,
                msg: '获取植物的属的数据成功',
                total: result[0].count
            }
            db_mysql.select(doorSql, null, results => {
                if (results && results.length > 0) {
                    for (var k in results) {
                        var temp = {
                            genusId: results[k].genusId,
                            sectionId: results[k].sectionId,
                            genusCNName: results[k].genusCNName,
                            genusENName: results[k].genusENName
                        }
                        resData.data.push(temp);
                    }
                    res.send(resData);
                } else {
                    errorData.msg = '读取数据失败';
                    res.send(errorData);
                }
            })
        } else {
            errorData.msg = '暂无数据';
            res.send(errorData);
        }
    })
})
/**
 * 根据科获取属
 * */
router.get('/', (req, res) => {
    const sectionId = JSON.parse(req.query.sectionId);
    let doorSql = 'select * from genus where sectionId=?;'
    let resData = {
        data: [],
        status: 0,
        msg: '获取数据成功',
        total: 0
    }
    let len = sectionId.length - 1;
    sectionId.forEach((item, index) => {
        db_mysql.select(doorSql, item, results => {
            if (results && results.length > 0) {
                for (var k in results) {
                    var temp = {
                        genusId: results[k].genusId,
                        sectionId: results[k].sectionId,
                        genusCNName: results[k].genusCNName,
                        genusENName: results[k].genusENName
                    }
                    resData.total++;
                    resData.data.push(temp);
                }
            }
            if (index === len) {
                res.send(resData);
            }
        })
    })
})

module.exports = router;
